package com.example.demo2.action;

import com.example.demo2.define.CDEvents;
import com.example.demo2.define.CDStates;
import com.example.demo2.define.Variables;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.action.Action;
import reactor.core.publisher.Mono;

public class ClosedEntryAction implements Action<CDStates, CDEvents> {

    @Override
    public void execute(StateContext<CDStates, CDEvents> context) {
        if (context.getTransition() != null
                && context.getEvent() == CDEvents.PLAY
                && context.getTransition().getTarget().getId() == CDStates.CLOSED
                && context.getExtendedState().getVariables().get(Variables.CD) != null) {
            context.getStateMachine()
                    .sendEvent(Mono.just(MessageBuilder
                            .withPayload(CDEvents.PLAY).build()))
                    .subscribe();
        }
    }
}